<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    
    <title>Using via numpy.distutils &mdash; NumPy v1.18 Manual</title>
    
    <link rel="stylesheet" type="text/css" href="../_static/css/spc-bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../_static/css/spc-extend.css">
    <link rel="stylesheet" href="../_static/scipy.css" type="text/css" >
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" >
    <link rel="stylesheet" href="../_static/graphviz.css" type="text/css" >
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.18.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/js/copybutton.js"></script>
    <link rel="author" title="About these documents" href="../about.html" >
    <link rel="index" title="Index" href="../genindex.html" >
    <link rel="search" title="Search" href="../search.html" >
    <link rel="top" title="NumPy v1.18 Manual" href="../index.html" >
    <link rel="up" title="F2PY Users Guide and Reference Manual" href="index.html" >
    <link rel="next" title="Advanced F2PY usages" href="advanced.html" >
    <link rel="prev" title="Using F2PY" href="usage.html" > 
  </head>
  <body>
<div class="container">
  <div class="top-scipy-org-logo-header" style="background-color: #a2bae8;">
    <a href="../index.html">
      <img border=0 alt="NumPy" src="../_static/numpy_logo.png"></a>
    </div>
  </div>
</div>


    <div class="container">
      <div class="main">
        
	<div class="row-fluid">
	  <div class="span12">
	    <div class="spc-navbar">
              
    <ul class="nav nav-pills pull-left">
        <li class="active"><a href="https://numpy.org/">NumPy.org</a></li>
        <li class="active"><a href="https://numpy.org/doc">Docs</a></li>
        
        <li class="active"><a href="../index.html">NumPy v1.18 Manual</a></li>
        

          <li class="active"><a href="index.html" accesskey="U">F2PY Users Guide and Reference Manual</a></li> 
    </ul>
              
              
    <ul class="nav nav-pills pull-right">
      <li class="active">
        <a href="../genindex.html" title="General Index"
           accesskey="I">index</a>
      </li>
      <li class="active">
        <a href="advanced.html" title="Advanced F2PY usages"
           accesskey="N">next</a>
      </li>
      <li class="active">
        <a href="usage.html" title="Using F2PY"
           accesskey="P">previous</a>
      </li>
    </ul>
              
	    </div>
	  </div>
	</div>
        

	<div class="row-fluid">
      <div class="spc-rightsidebar span3">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="usage.html"
                        title="previous chapter">Using F2PY</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="advanced.html"
                        title="next chapter">Advanced F2PY usages</a></p>
<div id="searchbox" style="display: none" role="search">
  <h4>Quick search</h4>
    <div>
    <form class="search" action="../search.html" method="get">
      <input type="text" style="width: inherit;" name="q" />
      <input type="submit" value="search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
          <div class="span9">
            
        <div class="bodywrapper">
          <div class="body" id="spc-section-body">
            
  <div class="section" id="using-via-numpy-distutils">
<h1>Using via <a class="reference internal" href="../reference/distutils.html#module-numpy.distutils" title="numpy.distutils"><code class="xref py py-obj docutils literal notranslate"><span class="pre">numpy.distutils</span></code></a><a class="headerlink" href="#using-via-numpy-distutils" title="Permalink to this headline">¶</a></h1>
<p><a class="reference internal" href="../reference/distutils.html#module-numpy.distutils" title="numpy.distutils"><code class="xref py py-mod docutils literal notranslate"><span class="pre">numpy.distutils</span></code></a> is part of NumPy extending standard Python <code class="docutils literal notranslate"><span class="pre">distutils</span></code>
to deal with Fortran sources and F2PY signature files, e.g. compile Fortran
sources, call F2PY to construct extension modules, etc.</p>
<div class="topic">
<p class="topic-title">Example</p>
<p>Consider the following <a class="reference external" href="setup_example.py">setup file</a>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">division</span><span class="p">,</span> <span class="n">absolute_import</span><span class="p">,</span> <span class="n">print_function</span>

<span class="kn">from</span> <span class="nn">numpy.distutils.core</span> <span class="kn">import</span> <span class="n">Extension</span>

<span class="n">ext1</span> <span class="o">=</span> <span class="n">Extension</span><span class="p">(</span><span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;scalar&#39;</span><span class="p">,</span>
                 <span class="n">sources</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;scalar.f&#39;</span><span class="p">])</span>
<span class="n">ext2</span> <span class="o">=</span> <span class="n">Extension</span><span class="p">(</span><span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;fib2&#39;</span><span class="p">,</span>
                 <span class="n">sources</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;fib2.pyf&#39;</span><span class="p">,</span> <span class="s1">&#39;fib1.f&#39;</span><span class="p">])</span>

<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">numpy.distutils.core</span> <span class="kn">import</span> <span class="n">setup</span>
    <span class="n">setup</span><span class="p">(</span><span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;f2py_example&#39;</span><span class="p">,</span>
          <span class="n">description</span>       <span class="o">=</span> <span class="s2">&quot;F2PY Users Guide examples&quot;</span><span class="p">,</span>
          <span class="n">author</span>            <span class="o">=</span> <span class="s2">&quot;Pearu Peterson&quot;</span><span class="p">,</span>
          <span class="n">author_email</span>      <span class="o">=</span> <span class="s2">&quot;pearu@cens.ioc.ee&quot;</span><span class="p">,</span>
          <span class="n">ext_modules</span> <span class="o">=</span> <span class="p">[</span><span class="n">ext1</span><span class="p">,</span> <span class="n">ext2</span><span class="p">]</span>
          <span class="p">)</span>
<span class="c1"># End of setup_example.py</span>
</pre></div>
</div>
<p>Running</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">setup_example</span><span class="o">.</span><span class="n">py</span> <span class="n">build</span>
</pre></div>
</div>
<p>will build two extension modules <code class="docutils literal notranslate"><span class="pre">scalar</span></code> and <code class="docutils literal notranslate"><span class="pre">fib2</span></code> to the
build directory.</p>
</div>
<p><a class="reference internal" href="../reference/distutils.html#module-numpy.distutils" title="numpy.distutils"><code class="xref py py-mod docutils literal notranslate"><span class="pre">numpy.distutils</span></code></a> extends <code class="docutils literal notranslate"><span class="pre">distutils</span></code> with the following features:</p>
<ul>
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">Extension</span></code> class argument <code class="docutils literal notranslate"><span class="pre">sources</span></code> may contain Fortran source
files. In addition, the list <code class="docutils literal notranslate"><span class="pre">sources</span></code> may contain at most one
F2PY signature file, and then the name of an Extension module must
match with the <code class="docutils literal notranslate"><span class="pre">&lt;modulename&gt;</span></code> used in signature file.  It is
assumed that an F2PY signature file contains exactly one <code class="docutils literal notranslate"><span class="pre">python</span>
<span class="pre">module</span></code> block.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">sources</span></code> does not contain a signature files, then F2PY is used
to scan Fortran source files for routine signatures to construct the
wrappers to Fortran codes.</p>
<p>Additional options to F2PY process can be given using <code class="xref py py-class docutils literal notranslate"><span class="pre">Extension</span></code>
class argument <code class="docutils literal notranslate"><span class="pre">f2py_options</span></code>.</p>
</li>
<li><p>The following new <code class="docutils literal notranslate"><span class="pre">distutils</span></code> commands are defined:</p>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">build_src</span></code></dt><dd><p>to construct Fortran wrapper extension modules, among many other things.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">config_fc</span></code></dt><dd><p>to change Fortran compiler options</p>
</dd>
</dl>
<p>as well as <code class="docutils literal notranslate"><span class="pre">build_ext</span></code> and  <code class="docutils literal notranslate"><span class="pre">build_clib</span></code> commands are enhanced
to support Fortran sources.</p>
<p>Run</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">&lt;</span><span class="n">setup</span><span class="o">.</span><span class="n">py</span> <span class="n">file</span><span class="o">&gt;</span> <span class="n">config_fc</span> <span class="n">build_src</span> <span class="n">build_ext</span> <span class="o">--</span><span class="n">help</span>
</pre></div>
</div>
<p>to see available options for these commands.</p>
</li>
<li><p>When building Python packages containing Fortran sources, then one
can choose different Fortran compilers by using <code class="docutils literal notranslate"><span class="pre">build_ext</span></code>
command option <code class="docutils literal notranslate"><span class="pre">--fcompiler=&lt;Vendor&gt;</span></code>. Here <code class="docutils literal notranslate"><span class="pre">&lt;Vendor&gt;</span></code> can be one of the
following names:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">absoft</span> <span class="n">sun</span> <span class="n">mips</span> <span class="n">intel</span> <span class="n">intelv</span> <span class="n">intele</span> <span class="n">intelev</span> <span class="n">nag</span> <span class="n">compaq</span> <span class="n">compaqv</span> <span class="n">gnu</span> <span class="n">vast</span> <span class="n">pg</span> <span class="n">hpux</span>
</pre></div>
</div>
<p>See <code class="docutils literal notranslate"><span class="pre">numpy_distutils/fcompiler.py</span></code> for up-to-date list of
supported compilers or run</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">f2py</span> <span class="o">-</span><span class="n">c</span> <span class="o">--</span><span class="n">help</span><span class="o">-</span><span class="n">fcompiler</span>
</pre></div>
</div>
</li>
</ul>
</div>


          </div>
        </div>
          </div>
        </div>
      </div>
    </div>

    <div class="container container-navbar-bottom">
      <div class="spc-navbar">
        
      </div>
    </div>
    <div class="container">
    <div class="footer">
    <div class="row-fluid">
    <ul class="inline pull-left">
      <li>
        &copy; Copyright 2008-2019, The SciPy community.
      </li>
      <li>
      Last updated on Feb 20, 2020.
      </li>
      <li>
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.4.2.
      </li>
    </ul>
    </div>
    </div>
    </div>
  </body>
</html>